package TLF 
{	
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;
	import flash.text.engine.FontWeight;
	import flash.utils.setTimeout;
	import flashx.textLayout.container.*;
	import flashx.textLayout.container.ContainerController;
	import flashx.textLayout.conversion.ConversionType;
	import flashx.textLayout.conversion.TextConverter;
	import flashx.textLayout.edit.EditManager;
	import flashx.textLayout.edit.SelectionManager;
	import flashx.textLayout.elements.*;
	import flashx.textLayout.elements.ParagraphElement;
	import flashx.textLayout.elements.SpanElement;
	import flashx.textLayout.elements.TextFlow;
	import iscript.coreFramework.data.compress.LZW;
//	import iscript.coreFramework.data.compress.*;
	
	public class TLFTest extends Sprite
	{
		private var textFlow:TextFlow = new TextFlow();
		private var textFlow2:TextFlow = new TextFlow();
		private var p:ParagraphElement = new ParagraphElement();
		private var  cc:ContainerController;
		
		private var _stage:Stage;
		
		public function TLFTest() 
		{
			_stage = this.stage;
			var container:Sprite=new Sprite();
				this.addChild(container);
				container.x = 40;
				container.y = 50;
			
			cc  = new ContainerController(container, 400, 400);
			textFlow.addChild(p);
			
			var span:SpanElement = new SpanElement();
			span.text = "Hello, World";
			//span.fontSize = 48;
			p.addChild(span);
		
		   var inlineGraphic:InlineGraphicElement = new InlineGraphicElement();
		   inlineGraphic.width = 50
		   inlineGraphic.height=50
		   inlineGraphic.source = "tttt.swf" //xiao;//"tttt.swf"
		  // Sprite(inlineGraphic.source).addEventListener(MouseEvent.CLICK,function(e:*):void{trace("test123123123")})
		   p.addChild(inlineGraphic);
			
		   
		 
	
		   
	
				
				  
				for (var i:int = 0; i < 	2; i++) 
				{
						  	var linkspan:SpanElement = new SpanElement();
			    linkspan.text = "百度       ";
		        //linkspan.color = 0xFF0000;
 	            linkspan.textDecoration = "none";
		        var  link:LinkElement = new LinkElement();
		          // link.lineThrough = true;
			         link.id="xxxxxxx"
				     link.fontWeight = FontWeight.BOLD;
				//link.target = "_blank";
				//link.href = "http://www.baidu.com";
					link.addChild(linkspan);
					link.addEventListener(MouseEvent.CLICK, function(e:*):void {					
						// trace(link.parent.parent)
						  
						trace(_stage.mouseX,_stage.mouseY, link.id)
						//trace(  FlowElementEventDispatcher(e.target)._element.parent.parent);
						})
				
				p.addChild(link)
		   
 
				}
		   
		    var selectmanager:SelectionManager = new SelectionManager();
			 textFlow.interactionManager = selectmanager;			
		     var editMode:EditManager = new EditManager();			
			 textFlow.interactionManager = editMode;
			 textFlow.interactionManager=null;
			 textFlow.flowComposer.addController(cc);
			 textFlow.flowComposer.updateAllControllers();
			
			
					
			/*	var mytxt:TextField=new TextField();
				addChild(mytxt);
				mytxt.textColor=0xffffff
				mytxt.text="测试描边文字";

				var txtGF:GlowFilter=new GlowFilter(0x000000,1,2,2,16);
				var txtGF2:GlowFilter=new GlowFilter(0xff0000,1,3,3,16);
				var myGF_arr:Array=new Array();
				myGF_arr.push(txtGF);
				myGF_arr.push(txtGF2);


				mytxt.filters = myGF_arr;*/

		
			//(source:TextFlow, format:String, conversionType:String, config:ImportExportConfiguration = null):Object ;
			
				var tt:String =	TextConverter.export(textFlow, TextConverter.TEXT_LAYOUT_FORMAT,  ConversionType.STRING_TYPE) as String;
				trace(tt.length)
				var s:String = LZW.compress(tt);
				trace(s.length)
				trace(s)
			
		
			var sprite:Sprite = new Sprite();

			var textFlow_1:TextFlow = new TextFlow();

			var span:SpanElement = new SpanElement();

			var paragraph:ParagraphElement = new ParagraphElement();

			paragraph.addChild(span);

			textFlow_1.addChild(paragraph);

			textFlow_1.fontSize = "32";

			var filterArray:Array = new Array();

			filterArray.push(new DropShadowFilter(3, 45, 0x000000, 0.75, 3, 3, 0.75, 3));

			this.filters = filterArray;
				
			//textFlow_1.flowComposer.addController(new ContainerController( sprite, 500, 300));

		//	textFlow_1.flowComposer.updateAllControllers();
			
				
				
			setTimeout(function():void { 		
				textFlow.addChild(textFlow2.getChildAt(0));
				textFlow.flowComposer.updateAllControllers();
				//trace( textFlow2.getChildAt(0))
				}, 3000);
			//setInterval(_enterFrameLogic, 1000);
			//addEventListener(Event.ENTER_FRAME,_enterFrameLogic)
			
			//contentHeight = controller.getContentBounds().height;
			
			stage.addEventListener(MouseEvent.CLICK, function(e:*):void{
				cc.verticalScrollPosition = 0;
				})
		}
		private  var n:int=0
		private function _enterFrameLogic():void 
		{
		var tt:String =	TextConverter.export(textFlow, TextConverter.TEXT_LAYOUT_FORMAT,  ConversionType.STRING_TYPE) as String;
			var textFlow2:TextFlow= TextConverter.importToFlow(tt, TextConverter.TEXT_LAYOUT_FORMAT);
             trace(this.textFlow.getChildAt(0))
			// textFlow.flowComposer.removeAllControllers();
			 	textFlow.addChild(textFlow2.getChildAt(0));
				textFlow.flowComposer.updateAllControllers();
			  
		    for (var j:int = 0; j <  p.numChildren-1; j++) 
			{				
				if (p.getChildAt(j) is SpanElement) {
					
				trace(SpanElement(p.getChildAt(j)).text)
					}
					
				if (p.getChildAt(j) is InlineGraphicElement) {
				
				 trace(InlineGraphicElement(p.getChildAt(j)).source)
					}
				
					//trace( InlineGraphicElement(p.getChildAt(j)).source);
			}

			n++;
			//if (n > 7) {
			//	this.textFlow.removeChildAt(0)
				
			//while (p.numChildren > 0) {
				 // p.removeChildAt(0)
			//	}
			
			//}
			
			// var span:SpanElement = new SpanElement();
			//span.text = "Hello, World";
			//span.fontSize = 48;
	 
			//p.addChild(span);
			 //	textFlow.flowComposer.updateAllControllers();
			//textFlow.flowComposer.getControllerAt(0).verticalScrollPosition = int.MAX_VALUE;
		}
		
	}

}